package com.example.web03homework.exception;

import com.example.web03homework.result.Result;
import com.example.web03homework.result.ResultCode;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理类
 */
@RestControllerAdvice   // 全局异常处理注释
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e){
        Result result = null;
        // 处理
        if(e instanceof ServiceException se){
            result = Result.error(se.getResultCode());
        }
        else{
            //返回500错误
            result = Result.error(ResultCode.FAILURE);
        }
        return result;
    }
}